package com.entrogy.controller;

import com.entrogy.bean.PubCountEntity;
import com.entrogy.dao.MessageMapper;
import com.entrogy.service.StudentService;
import com.entrogy.utils.ApiReturnUtil;
import com.entrogy.utils.SendUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * @author xlr
 * @date 2019-07-11
 * @description 家长忘记孩子的接送 让其自行离校
 */
@Api(description = "家长接送孩子的接口（家长忘记接孩子后，可以在其小程序上设置让其出校）")
@RestController
@RequestMapping(value = "/pickstudent")
public class PickStudentController {

    private static final Logger logger = LoggerFactory.getLogger(PickStudentController.class);

    @Autowired
    private StudentService studentService;
    @Autowired
    private MessageMapper messageMapper;
    @ApiOperation(value = "家长让孩子出门")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "用户ID",name = "uid",paramType = "query",dataType = "String",required = true),
            @ApiImplicitParam(value = "学生ID",name = "stuId",paramType = "query",dataType = "String",required = true)
    })
    @RequestMapping(value = "/gohome",method = RequestMethod.POST)
    public Map<String,Object> setStudentGoHome(String uid, String stuId, HttpServletRequest request){
        logger.info("正在进行家长设置离校接口的调用，uid：{}，stuId：{}，IP地址：{}", uid, stuId, getIp(request));
        // 通知管理员
        String[] paramsWeiXin = { "",  "系统", "系统", new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss").format(new Date()), "正在进行家长设置离校接口的调用，uid：{}，stuId：{}，IP地址：{}", uid, stuId, getIp(request), ""};
        PubCountEntity pubCountEntity = messageMapper.getPubCountOpenidByPhone("17628011691");
        // path参数为点击公众号消息后跳转的地址
        SendUtils.sendByWeixin(pubCountEntity, paramsWeiXin, pubCountEntity.getTemplateId());
        //        int code = studentService.setStudentGoHome(uid, stuId);
//        if(code == 1){
            return ApiReturnUtil.success("设置成功","");
//        }
//        return ApiReturnUtil.error("网络异常");
    }

    private String getIp(HttpServletRequest request) {
        String ip = request.getHeader("X-Forwarded-For");
        if(StringUtils.isNotEmpty(ip) && !"unKnown".equalsIgnoreCase(ip)){
            int index = ip.indexOf(",");
            if(index != -1){
                return ip.substring(0,index);
            }else{
                return ip;
            }
        }
        ip = request.getHeader("X-Real-IP");
        if(StringUtils.isNotEmpty(ip) && !"unKnown".equalsIgnoreCase(ip)){
            return ip;
        }
        return request.getRemoteAddr();
    }
}
